////////////////////////////////////////////////////////////////////////////////
//   Copyright GRGBanking Platform 2011 
//   All rights reserved. 
////////////////////////////////////////////////////////////////////////////////
package utils
{

	/*========================================================*/
	/*   Import                                               */
	/*========================================================*/

	import mx.formatters.DateFormatter;

	public class DateFormatter extends mx.formatters.DateFormatter
	{

		/*========================================================*/
		/*   Constructor                                          */
		/*========================================================*/

		public function DateFormatter()
		{
			super();
		}

		/*========================================================*/
		/*   Static Property                                      */
		/*========================================================*/

		public static var formatString:String = "YYYY-MM-DD JJ:NN:SS";

		/*========================================================*/
		/*   Static Function                                      */
		/*========================================================*/

		/**
		 * 按指定的字符串格式把时间转换成字符串
		 */
		public static function format(date:Object, formatStr:String = null):String
		{
			if(date == null || date == "")
				return "";
			try
			{
				var dateFormat:mx.formatters.DateFormatter = new mx.formatters.DateFormatter();
				dateFormat.formatString = formatStr ? formatStr : formatString;
				return dateFormat.format(date);
			}
			catch (e:Error)
			{
				trace("时间格式转换错误：" + e.message);
			}
			return "";
		}

		public static function formatDate(date:Object):String
		{
			if(date == null || date == "")
				return "";
			var dateFormat:mx.formatters.DateFormatter = new mx.formatters.DateFormatter();
			dateFormat.formatString = "YYYY-MM-DD";
			return dateFormat.format(date);
		}

		public static function formatDateTime(date:Object):String
		{
			if(date == null || date == "")
				return "";
			var dateFormat:mx.formatters.DateFormatter = new mx.formatters.DateFormatter();
			dateFormat.formatString = formatString;
			return dateFormat.format(date);
		}

		public static function getDay(date:Object):String
		{
			if(date == null || date == "")
				return "";
			var dateFormat:mx.formatters.DateFormatter = new mx.formatters.DateFormatter();
			dateFormat.formatString = "DD";
			return dateFormat.format(date);
		}

		public static function getMonth(date:Object):String
		{
			if(date == null || date == "")
				return "";
			var dateFormat:mx.formatters.DateFormatter = new mx.formatters.DateFormatter();
			dateFormat.formatString = "MM";
			return dateFormat.format(date);
		}

		public static function getYear(date:Object):String
		{
			if(date == null || date == "")
				return "";
			var dateFormat:mx.formatters.DateFormatter = new mx.formatters.DateFormatter();
			dateFormat.formatString = "YYYY";
			return dateFormat.format(date);
		}

		public static function getYearMonthDay(date:Object):String
		{
			if(date == null || date == "")
				return "";
			var dateFormat:mx.formatters.DateFormatter = new mx.formatters.DateFormatter();
			dateFormat.formatString = "YYYYMMDD";
			return dateFormat.format(date);
		}

		public static function parseDate(date:String):Date
		{
			if(date == null || date == "")
				return null;
			return mx.formatters.DateFormatter.parseDateString(date);
		}
	}
}